/**
 * 必须包含列出的所有权限，元素才显示
 */
import React, { Component } from 'react';
import PropTypes from 'prop-types'
import db from '@/utils/localstorage';

export let HasPermission = ComposedComponent =>class WrapComponent extends Component {
    constructor(props) {
      super(props);
    }
  
    // static propTypes = {
    //   auth:PropTypes.string.isRequired,
    // };
    hasPermission=()=>{
      let permissions = db.get('permissions');
      if(!permissions||Object.keys(permissions).length==0){
        permissions=[]
      }
      const value = this.props.auth;
      if(value){
        let flag = true
        for (const v of value) {
          if (!permissions.includes(v)) {
            flag = false
          }
        }
       return flag
      }else{
        const valueOr = this.props.authOr;
        if(valueOr){
          let flag = true
          for (const vOr of valueOr) {
            for(const v of vOr){
              if (!permissions.includes(v)) {
                flag = false
                break;
              }
              if(flag){
                break;
              }
            }
          }
         return flag
        }
      }
    }
    render() {
      //如果权限有值，并且具备权限，那么创建按钮
      return this.hasPermission()?<ComposedComponent { ...this.props} />:null;
    }
  };